package com.baoteng.rbspy.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.Maps;

public class DateUtil {

	public final static int DELIMITER_FORMAT = 1;

	public final static int COMPACT_FORMAT = 2;

	private final static Map<Integer, String[]> ACCEPTABLE_FORMATS = Maps.newHashMap();

	private final static Map<Integer, String[]> DEFAULT_PATTERN = Maps.newHashMap();

	static {
		ACCEPTABLE_FORMATS.put(DELIMITER_FORMAT, new String[] { "yyyy-MM", "yyyy-MM-dd", "yyyy-MM-dd HH:mm",
				"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ssX", "yyyy-MM-ddX" });
		DEFAULT_PATTERN.put(DELIMITER_FORMAT,
				new String[] { "^\\d{4}-\\d{1,2}$", "^\\d{4}-\\d{1,2}-\\d{1,2}$",
						"^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$",
						"^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$",
						"^\\d{4}-\\d{1,2}-\\d{1,2}T{1}\\d{1,2}:\\d{1,2}:\\d{1,2}\\+\\d{1,2}:\\d{1,2}$",
						"^\\d{4}-\\d{1,2}-\\d{1,2}\\+\\d{1,2}:\\d{1,2}$" });
		ACCEPTABLE_FORMATS.put(COMPACT_FORMAT, new String[] { "yyyyMM", "yyyyMMdd", "yyyyMMddHHmm", "yyyyMMddHHmmss",
				"yyyyMMdd'T'HHmmssX", "yyyyMMddX" });
		DEFAULT_PATTERN.put(COMPACT_FORMAT, new String[] { "^\\d{6}$", "^\\d{8}$", "^\\d{12}$", "^\\d{14}$",
				"^\\d{8}T{1}\\d{6}\\+\\d{1,2}:\\d{1,2}$", "^\\d{8}+\\d{1,2}:\\d{1,2}$" });
	}

	public static Date fromString(String dateStr) {
		if (StringUtils.isBlank(dateStr)) {
			return null;
		}
		int format=1;
		dateStr = dateStr.trim();
		if (dateStr.matches(DEFAULT_PATTERN.get(format)[0])) {
			try {
				return (Date) new SimpleDateFormat(ACCEPTABLE_FORMATS.get(format)[0]).parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		} else if (dateStr.matches(DEFAULT_PATTERN.get(format)[1])) {
			try {
				return (Date) new SimpleDateFormat(ACCEPTABLE_FORMATS.get(format)[1]).parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		} else if (dateStr.matches(DEFAULT_PATTERN.get(format)[2])) {
			try {
				return (Date) new SimpleDateFormat(ACCEPTABLE_FORMATS.get(format)[2]).parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		} else if (dateStr.matches(DEFAULT_PATTERN.get(format)[3])) {
			try {
				return (Date) new SimpleDateFormat(ACCEPTABLE_FORMATS.get(format)[3]).parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		} else if (dateStr.matches(DEFAULT_PATTERN.get(format)[4])) {
			try {
				return (Date) new SimpleDateFormat(ACCEPTABLE_FORMATS.get(format)[4]).parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		} else if (dateStr.matches(DEFAULT_PATTERN.get(format)[5])) {
			try {
				return (Date) new SimpleDateFormat(ACCEPTABLE_FORMATS.get(format)[5]).parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		} else {
			 throw new IllegalArgumentException("Invalid source value '" + dateStr + "'");
		}
	}
	
	public static Date fromString(String dateStr,int format) {
		if (StringUtils.isBlank(dateStr)) {
			return null;
		}
		dateStr = dateStr.trim();
		if (dateStr.matches(DEFAULT_PATTERN.get(format)[0])) {
			try {
				return (Date) new SimpleDateFormat(ACCEPTABLE_FORMATS.get(format)[0]).parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		} else if (dateStr.matches(DEFAULT_PATTERN.get(format)[1])) {
			try {
				return (Date) new SimpleDateFormat(ACCEPTABLE_FORMATS.get(format)[1]).parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		} else if (dateStr.matches(DEFAULT_PATTERN.get(format)[2])) {
			try {
				return (Date) new SimpleDateFormat(ACCEPTABLE_FORMATS.get(format)[2]).parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		} else if (dateStr.matches(DEFAULT_PATTERN.get(format)[3])) {
			try {
				return (Date) new SimpleDateFormat(ACCEPTABLE_FORMATS.get(format)[3]).parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		} else if (dateStr.matches(DEFAULT_PATTERN.get(format)[4])) {
			try {
				return (Date) new SimpleDateFormat(ACCEPTABLE_FORMATS.get(format)[4]).parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		} else if (dateStr.matches(DEFAULT_PATTERN.get(format)[5])) {
			try {
				return (Date) new SimpleDateFormat(ACCEPTABLE_FORMATS.get(format)[5]).parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		} else {
			 throw new IllegalArgumentException("Invalid source value '" + dateStr + "'");
		}
	}
}
